package com.xudow.app.dynamicstate_old.module.discovery.viewholder;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.tagview.TAGView;
import com.jude.utils.JUtils;
import com.squareup.picasso.Picasso;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.dynamicstate_old.data.DynamicModel;
import com.xudow.app.dynamicstate_old.data.ImageModel;
import com.xudow.app.dynamicstate_old.data.server.ErrorTransform;
import com.xudow.app.dynamicstate_old.domain.entity.UserInfoVo;
import com.xudow.app.dynamicstate_old.module.user.UserProfileActivity;
import com.xudow.app.newui.PreLoginActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by zhuchenxi on 16/6/24.
 */

public class UserSearchViewHolder extends BaseViewHolder<UserInfoVo> {
    public UserSearchViewHolder(ViewGroup parent) {
        super(parent, R.layout.item_search_user);
        ButterKnife.bind(this, itemView);
    }

    @BindView(R.id.photo)
    CircleImageView photo;
    @BindView(R.id.text_name)
    TextView textName;
    @BindView(R.id.tag_follow)
    TAGView tagFollow;


    @Override
    public void setData(UserInfoVo data) {
        if (TextUtils.isEmpty(data.getHeadUrl())) {
            Picasso.with(getContext()).load(R.drawable.default_head_img).into(photo);
        } else
            Picasso.with(getContext()).load(ImageModel.getXueDouImage(data.getHeadUrl())).resize(300, 300).into(photo);
        textName.setText(data.getUserName());
        int color = getContext().getResources().getColor(R.color.state_bar_color);
        if (data.getIsAttended() == 1) {
            tagFollow.setStrokeWidth(0);
            tagFollow.setTextColor(Color.WHITE);
            tagFollow.setBackgroundColor(color);
            tagFollow.setText("已关注");
        } else {
            tagFollow.setStrokeWidth(1);
            tagFollow.setTextColor(color);
            tagFollow.setBackgroundColor(color);
            tagFollow.setText("关注");
        }
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), UserProfileActivity.class);
                i.putExtra(UserProfileActivity.USER_PROFILE_ID, data.getUserProfileId());
                ((Activity) getContext()).startActivity(i);
            }
        });
        tagFollow.setOnClickListener(i -> {
            if (!XApplication.getInstance().isLogin()) {
                getContext().startActivity(new Intent(getContext(), PreLoginActivity.class));
                return;
            }
            if (data.getIsAttended() == 1) {
                DynamicModel.getInstance().unattention(XApplication.getInstance().getUserProfileId())
                        .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.TOAST))
                        .subscribe(message -> {
                            data.setIsAttended(2);
                            JUtils.Toast("已取消关注");
                            tagFollow.setStrokeWidth(1);
                            tagFollow.setTextColor(color);
                            tagFollow.setBackgroundColor(color);
                            tagFollow.setText("关注");
                        });
            } else {
                DynamicModel.getInstance().attention(data.getUserProfileId(), data.getUsertype(), XApplication.getInstance().isStudent() ? 2 : 1)
                        .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.TOAST))
                        .subscribe(message -> {
                            data.setIsAttended(1);
                            JUtils.Toast("关注成功");
                            tagFollow.setStrokeWidth(0);
                            tagFollow.setTextColor(Color.WHITE);
                            tagFollow.setBackgroundColor(color);
                            tagFollow.setText("已关注");
                        });
            }
        });
    }
}
